/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package conn.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author root
 */
public class DDireccion extends DBase {

    public int getUltimoId() {
        Statement stmt = null;
        ResultSet res = null;

        this.init_mysql();  // se inicia la conección a la base de datos
        int id = 0;
        try {

            stmt = conn.createStatement();
            res = stmt.executeQuery("SELECT MAX(idDireccion) from direccion");

            if (res.next()) {

                id = res.getInt(1);
            }

            res.close();
            stmt.close();
            conn.close();

        } catch (SQLException ex) {
            System.out.println("SqlException : " + ex.toString());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.toString());
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    ;
                }
                res = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    ;
                }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    ;
                }
                conn = null;
            }
        }
        return id;
    }

    public void crearDireccion(String direccion, int idDistrito) {
        Statement stmt = null;
        ResultSet res = null;

        this.init_mysql();  // se inicia la conección a la base de datos

        try {

            stmt = conn.createStatement();
            stmt.executeUpdate(""
                    + " INSERT INTO direccion "
                    + " (descripcion, idDistrito)"
                    + " VALUES "
                    + " ('"+direccion+"',"+idDistrito+");");

            stmt.close();
            conn.close();

        } catch (SQLException ex) {
            System.out.println("SqlException : " + ex.toString());
        } catch (Exception ex) {
            System.out.println("Exception : " + ex.toString());
        } finally {
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    ;
                }
                res = null;
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    ;
                }
                stmt = null;
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    ;
                }
                conn = null;
            }
        }
    }
}
